import api from '../../../utils/api.js'
import util from '../../../utils/util.js'

const isAudio = (path) => {
    return /\.(opus|flac|webm|weba|wav|ogg|m4a|mp3|oga|mid|amr|aiff|wma|au|aac)/.test(
        path
    )
}

const isVideo = (path) => {
    return /\.(mp4|avi|wmv|mpg|mpeg|mov|rm|ram|swf|flv)/.test(path)
}
Page({
    data: {
        articleId: '',
        articleInfo: {
            content: '',
        },
        videoFlag: false,
        audioFlag: false,
        videoInfo: {}, // 视频
        audioInfo: {}, // 音频
        recentAudio: {}, // 播放轨迹记录使用
    },
    onLoad: function (options) {
        this.setData({
            articleId: options.articleId,
        })
        this.getArticleInfo(options.articleId)

    },
      /**播放视屏 */
      play(e) {
        //执行全屏方法
        var videoContext = wx.createVideoContext('myvideo', this);
        videoContext.requestFullScreen();
        this.setData({
            fullScreen:true
        })
     },
     /**关闭视屏 */
     closeVideo() {
       //执行退出全屏方法
       var videoContext = wx.createVideoContext('myvideo', this);
       videoContext.exitFullScreen();
     },
     /**视屏进入、退出全屏 */
     fullScreen(e){
       var isFull = e.detail.fullScreen;
       //视屏全屏时显示加载video，非全屏时，不显示加载video
       this.setData({
         fullScreen:isFull
       })
     },
    onReady: function () {},
    onShow: function () {
        // let audioObj = wx.getStorageSync('healthplanAudio')
        // if (audioObj) {
        //     this.setData({
        //         recentAudio: audioObj,
        //     })
        // }
    },
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {},
    getArticleInfo(articleId) {
        if (articleId) {
            this.data.articleId = articleId
        }
        let article_id = this.data.articleId
        let url = `/api/hxhealth/video/load/${article_id}`
        api.get(url)
            .then((res) => {
                console.log('---获取文章详情---', res)
                if (res.length > 0) {
                    // let imgReg = /<img.*?(?:>|\/>)/gi //匹配图片中的img标签
                    // if (imgReg.test(res[0].content)) {
                    //     // res[0].content = util.htmlFormatForSel(res[0].content)
                    // }

                    if (res[0].video && isVideo(res[0].video)) {
                        this.setData({
                            videoFlag: true,
                            videoInfo: {
                                title: res[0].name,
                                videoUrl: res[0].video,
                                coverUrl: res[0].icon,
                                collect: res[0].collect
                            },
                        })
                        wx.setNavigationBarTitle({
                            title: res[0].name?res[0].name:'视频播放'
                        })
                    }

                    if (res[0].video && isAudio(res[0].video)) {
                        this.setData({
                            audioFlag: true,
                            audioInfo: {
                                title: '健康计划音频',
                                src: res[0].video,
                                coverImgUrl: res[0].icon,
                            },
                        })
                    }

                    this.setData({
                        articleInfo: res[0],
                        link: res[0].link,
                        video: res[0].video
                    })
                    setTimeout(()=>{
                        this.play()
                      },2000)

                }
            })
            .catch((error) => {
                console.log('---获取文章详情 catch---', error)
            })
    },
    toChangeState() {
        let article_id = this.data.articleId
        let url = ''
        console.log(this.data.videoInfo.collect);
        if(this.data.videoInfo.collect == '1') {
            url = `/api/hxhealth/video/uncollect/${article_id}`
        } else {
            url = `/api/hxhealth/video/collect/${article_id}`
        }
        api.get(url)
            .then((res) => {
                console.log('---改变状态---', res)
                this.getArticleInfo()
            })
            .catch((error) => {
                console.log('---改变状态 catch---', error)
            })
    }
})
